package com.zxm.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxm.ssm.entity.RegisterUser;
import com.zxm.ssm.entity.User;
import com.zxm.ssm.mapper.UserMapper;
import com.zxm.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ZXM
 * @ClassName UserServiceImpl
 * @Description TODO
 * @date 2021/7/18 10:21
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByNameAndPwd(User user) {
        return userMapper.selectUserByIdAndPwd(user.getUsername(), user.getPassword());
    }

    @Override
    public Integer addUser(RegisterUser registerUser) {
        User user = new User();
        user.setAge(0);
        user.setGender(0);
        user.setMoney(new Float(0));
        user.setRole(1);
        user.setUsername(registerUser.getName());
        user.setPassword(registerUser.getRepeatPassword());
        user.setCreateTime(new Date());
        return userMapper.addUser(user);
    }

    @Override
    public PageInfo<User> findAllUsers(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public Integer saveUser(User user) {
        user.setRole(1);
        user.setCreateTime(new Date());
        user.setPassword("123456");
        return userMapper.save(user);
    }

    @Override
    public User findUserById(int id) {
        return userMapper.findById(id);
    }

    @Override
    public Integer UpdateUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public void delUserById(int id) {
        userMapper.delById(id);
    }
}
